﻿#region MIT
/*
Engine Of Evermore (http://code.google.com/p/engineofevermore/)
Copyright (c) 2008, 2009 Michael Woerister

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EngineOfEvermore.Editor.Common.Command;
using NUnit.Framework;
using EngineOfEvermore.Common;
using EngineOfEvermore.Scene;
using System.Diagnostics;

namespace EngineOfEvermore.Editor.SceneTools
{
    class MoveSceneObjectsCommand : ICommand
    {
        public Vector2d From { get; private set; }
        public Vector2d To { get; private set; }
        public IEnumerable<SceneObject> SceneObjects { get; private set; }

        public string Description
        {
            get { throw new NotImplementedException(); }
        }

        public MoveSceneObjectsCommand( IEnumerable<SceneObject> sceneObjects, Vector2d from,
            Vector2d to )
        {
            Debug.Assert( sceneObjects != null );

            this.SceneObjects = sceneObjects.ToArray();
            this.From = from;
            this.To = to;
        }

        public void Undo()
        {
            var offset = this.To - this.From;

            foreach ( var item in this.SceneObjects )
            {
                item.Position -= offset;
            }
        }

        public void Redo()
        {
            var offset = this.To - this.From;

            foreach ( var item in this.SceneObjects )
            {
                item.Position += offset;
            }
        }

        #if ENGINE_OF_EVERMORE_ENABLE_UNIT_TESTS
        [TestFixture]
        public class Test
        {
            [Test]
            public void TestCreation()
            {
                var sceneObjects = new SceneObject[] { new SceneObject() };
                var from = new Vector2d( 1, 1 );
                var to = new Vector2d( 2, 2 );

                var command = new MoveSceneObjectsCommand( sceneObjects, from, to );

                Assert.That( command.SceneObjects, Is.EquivalentTo( sceneObjects ) );
                Assert.That( command.From, Is.EqualTo( from ) );
                Assert.That( command.To, Is.EqualTo( to ) );
            }

            [Test]
            public void TestUndo()
            {
                var sceneObjects = Util.Generate( () => new SceneObject(), 2 ).ToArray();
                sceneObjects[0].Position = new Vector2d( 100, 100 );
                sceneObjects[1].Position = Vector2d.Zero;

                var from = new Vector2d( 10, 15 );
                var to = new Vector2d( 20, 0 );

                var offset = to - from;

                var expectedPositions = new Vector2d[] 
                {
                    sceneObjects[0].Position - offset,
                    sceneObjects[1].Position - offset
                };

                var command = new MoveSceneObjectsCommand( sceneObjects, from, to );
                command.Undo();

                Assert.That( sceneObjects[0].Position, Is.EqualTo( expectedPositions[0] ) );
                Assert.That( sceneObjects[1].Position, Is.EqualTo( expectedPositions[1] ) );
            }

            [Test]
            public void TestRedo()
            {
                var sceneObjects = Util.Generate( () => new SceneObject(), 2 ).ToArray();
                sceneObjects[0].Position = new Vector2d( 100, 100 );
                sceneObjects[1].Position = Vector2d.Zero;

                var from = new Vector2d( 10, 15 );
                var to = new Vector2d( 20, 0 );

                var expectedPositions = new Vector2d[] 
                {
                    sceneObjects[0].Position,
                    sceneObjects[1].Position
                };

                var command = new MoveSceneObjectsCommand( sceneObjects, from, to );
                command.Undo();
                command.Redo();

                Assert.That( sceneObjects[0].Position, Is.EqualTo( expectedPositions[0] ) );
                Assert.That( sceneObjects[1].Position, Is.EqualTo( expectedPositions[1] ) );
            }
        }
        #endif
    }
}
